using MediatR;
using Movies.Application.Mappers;
using Movies.Application.Queries;
using Movies.Application.Responses;
using Movies.Core.Repositories;

namespace Movies.Application.Handlers;

public class GetMoviesHandler : IRequestHandler<GetMoviesQuery, IEnumerable<MovieResponse>>
{
    private readonly IMovieRepository _movieRepository;

    public GetMoviesHandler(IMovieRepository movieRepository)
    {
        _movieRepository = movieRepository;
    }

    public async Task<IEnumerable<MovieResponse>> Handle(
        GetMoviesQuery request,
        CancellationToken cancellationToken
    )
    {
        var movieList = await _movieRepository.GetMovies();
        var movieResponseList = MovieMapper.Mapper.Map<IEnumerable<MovieResponse>>(movieList);
        return movieResponseList;
    }
}
